class Solution {
public:
    int splitArray(vector<int>& nums, int m) {
        int left = 0, right = 0;
        for (int num : nums) {
            left = max(left, num);
            right += num;
        }
        while (left < right) {
            int mid = (left + right) / 2;
            if (validate(nums, m, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }

private:
    bool validate(vector<int>& nums, int m, int size) {
        int box = 0;
        int count = 1;
        for (int num : nums) {
            if (box + num <= size) {
                box += num;
            } else {
                count++;
                box = num;
            }
        }
        return count <= m;
    }
};